package com.qiku.controller;

import com.github.pagehelper.PageInfo;
import com.qiku.domin.PromotionAd;
import com.qiku.domin.PromotionAdVO;
import com.qiku.domin.ResponseResult;
import com.qiku.service.PromotionAdService;
import com.qiku.service.PromotionSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {


    @Autowired
    private PromotionAdService promotionAdService;


    @RequestMapping(value = "/PromotionAdUpload", method = RequestMethod.POST)
    public ResponseResult PromotionAdUpload(@RequestParam("file") MultipartFile file, HttpServletRequest req) {

        try {
            if (file.isEmpty()) {
                throw new Exception("未找到图片信息");
            }
            // 获取项目部署的路径
            //  localhost:8080/webapps/ssm_web/
            String realPath = req.getServletContext().getRealPath("/");
            System.out.println(realPath);
            // 获取项目名称  \ssm_web  ==>  ssm_web
            String projectName = req.getContextPath().substring(1);
            System.out.println("projectName = " + projectName);
            // 获取上传文件的真实路径
            // localhost:8080/webapps    /upload
            String webappsPath = realPath.substring(0, realPath.indexOf(projectName));
            System.out.println("webappsPath = " + webappsPath );

            String imgPath = webappsPath + "/upload/";
            // 获取源文件名
            String filename = file.getOriginalFilename();

            // 设置新文件名 （随机字符串） 防止命名冲突   System.currentTimeMillis()  获取当前毫秒数
            String newFileName = System.currentTimeMillis() + filename;
            // 上传文件
            File filePath = new File(imgPath, newFileName);

            file.transferTo(filePath);
            // 返回结果集
            Map<String, String> map = new HashMap<>();
            map.put("fileName", newFileName);
            map.put("filePath", imgPath + newFileName);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }

    }

    @RequestMapping(value = "/saveOrUpdatePromotionAd" , method = RequestMethod.POST)
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd){
        ResponseResult result = null;
        if (null != promotionAd.getId() && 0 < promotionAd.getId()){
            // 有id 修改
            promotionAdService.updatePromotionAd(promotionAd);
            result = new ResponseResult(true , 200 , "修改成功" , null);
            return result;
        }else {
            promotionAdService.savePromotionAd(promotionAd);
            result = new ResponseResult(true , 200 , "保存成功" , null);
            return result;
        }

    }

    @RequestMapping(value = "/findPromotionAdById" , method = RequestMethod.GET)
    public ResponseResult findPromotionAdById(@RequestParam Integer id){
        PromotionAd promotionAdById = promotionAdService.findPromotionAdById(id);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", promotionAdById);
        return result;
    }


    @RequestMapping(value = "/updatePromotionAdStatus" , method = RequestMethod.GET)
    public ResponseResult updatePromotionAdStatus(@RequestParam Integer id , @RequestParam int status){
        promotionAdService.updatePromotionAdStatus(id , status);
        Map<String , Integer> map = new HashMap<>();
        Integer mapstatus = map.put("status", status);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", mapstatus);
        return result;
    }


    @RequestMapping(value = "findAllPromotionAd" , method = RequestMethod.GET)
    public ResponseResult findAllPromotionAdByPage(PromotionAdVO promotionAdVO){
        PageInfo<PromotionAd> byPage = promotionAdService.findAllPromotionAdByPage(promotionAdVO);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", byPage);
        return result;

    }





}
